package com.brite.android.game.achievement;

import java.util.ArrayList;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Display;
import android.view.KeyEvent;

import com.brite.android.game.achievement.model.data.dao.RankViewDAO;
import com.brite.android.game.achievement.model.orm.RankView;
import com.brite.android.game.action.Cordinate;
import com.brite.android.game.action.Region;
import com.brite.android.game.menu.MainMenu;

public class HighScoreActivity extends BaseGameActivity {

	private int CAMERA_WIDTH;
	private int CAMERA_HEIGHT;
	
	private Scene scene;
	
	private Texture bgTexture;
	private TextureRegion bgTextureRegion;

	private Texture backTexture;
	private TextureRegion backTextureRegion;
	
	private Texture textureFontIntroduction;
	private Font fontIntroduction;
	private Texture textureFontScore;
	private Font fontScore;
	
	private Texture monsterKilledTexture;
	private TextureRegion monsterKilledTextureRegion;
	
	private ChangeableText[] namePlayer = new ChangeableText[10];
	private ChangeableText[] scorePlayer = new ChangeableText[10];
	private ChangeableText[] monsterKilledByPlayer = new ChangeableText[10];
	private Sprite[] monsterKilledIcon = new Sprite[10];
	
	private static ArrayList<RankView> arrList;
	static RankViewDAO rankViewDAO;
	
	@Override
	public Engine onLoadEngine() {
		Display display = getWindowManager().getDefaultDisplay();
		CAMERA_WIDTH = display.getWidth();
		CAMERA_HEIGHT = display.getHeight();
		
		Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions eo = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
		eo.setNeedsMusic(true);
		eo.setNeedsSound(true);
		mEngine = new Engine(eo);
		
		return mEngine;
	}

	private void loadResourceAppearence(){
		backTexture = new Texture(128, 128, TextureOptions.DEFAULT);
		backTextureRegion = TextureRegionFactory.createFromAsset(backTexture, this, "backOptional.png", 0, 0);
		
		mEngine.getTextureManager().loadTextures(backTexture);
	}
	
	private void loadResourceScore(){
		textureFontIntroduction = new Texture(512, 512, TextureOptions.BILINEAR);
		fontIntroduction = new Font(textureFontIntroduction, Typeface.create(Typeface.MONOSPACE, Typeface.BOLD), 30, true, Color.RED );
		textureFontScore = new Texture(512, 512, TextureOptions.BILINEAR);
		fontScore = new Font(textureFontScore, Typeface.create(Typeface.MONOSPACE, Typeface.BOLD), 15, true, Color.BLUE );
//		FontFactory.createFromAsset(textFont, context, "font/Zrnic.ttf", 28, true, -1);
		
		monsterKilledTexture = new Texture(32, 32, TextureOptions.DEFAULT);
		monsterKilledTextureRegion = TextureRegionFactory.createFromAsset(monsterKilledTexture, this, "icon_monsterKilled.png", 0, 0);
		
		mEngine.getTextureManager().loadTextures(textureFontIntroduction, textureFontScore, monsterKilledTexture);
		mEngine.getFontManager().loadFonts(fontIntroduction, fontScore);
	}
	
	
	@Override
	public void onLoadResources() {
		
		TextureRegionFactory.setAssetBasePath("gfx/");

		bgTexture = new Texture(1024, 1024, TextureOptions.DEFAULT);
		bgTextureRegion = TextureRegionFactory.createFromAsset(bgTexture, this, "menu_hight_soccer(960x640).png", 0, 0);
		
		loadResourceScore();
		
		loadResourceAppearence();
		
		mEngine.getTextureManager().loadTextures(bgTexture);
	}

	@Override
	public Scene onLoadScene() {
		scene = new Scene(1);
		Sprite bgSprite = new Sprite(0, 0, bgTextureRegion);
		bgSprite.setWidth(CAMERA_WIDTH);
		bgSprite.setHeight(CAMERA_HEIGHT);
		scene.attachChild(bgSprite);

		rankViewDAO = new RankViewDAO(this);
		
		arrList = new ArrayList<RankView>();
		if(listRanking() != null){
			arrList = listRanking();
		}
		
		if (arrList.size() > 0) {
			RankView rankPlayer0 = arrList.get(0);
			
			namePlayer[0] = new ChangeableText(0, 0, fontScore, rankPlayer0.getUserName());
			namePlayer[0].setPosition(40, 75);
			scene.attachChild(namePlayer[0]);
			
			scorePlayer[0] = new ChangeableText(0, 0, fontScore, "Score| " + rankPlayer0.getScore());
			scorePlayer[0].setPosition(CAMERA_WIDTH/3, 75);
			scene.attachChild(scorePlayer[0]);
			
			monsterKilledIcon[0] = new Sprite(0, 0, monsterKilledTextureRegion.clone());
			monsterKilledIcon[0].setPosition(2*CAMERA_WIDTH/3, 75);
			monsterKilledIcon[0].setSize(20, 20);
			scene.attachChild(monsterKilledIcon[0]);
			
			monsterKilledByPlayer[0] = new ChangeableText(0, 0, fontScore, "| " + rankPlayer0.getNumberKill());
			monsterKilledByPlayer[0].setPosition(monsterKilledIcon[0].getX() + monsterKilledIcon[0].getWidthScaled(), 75);
			scene.attachChild(monsterKilledByPlayer[0]);
			
			for (int i = 1; i < arrList.size(); i++) {
				RankView rankPlayer = arrList.get(i);
				
				namePlayer[i] = new ChangeableText(0, 0, fontScore, rankPlayer.getUserName());
				namePlayer[i].setPosition(40, monsterKilledIcon[i-1].getY() + monsterKilledIcon[i-1].getHeightScaled());
				
				scorePlayer[i] = new ChangeableText(0, 0, fontScore, "Score| "+rankPlayer.getScore());
				scorePlayer[i].setPosition(CAMERA_WIDTH/3, monsterKilledIcon[i-1].getY() + monsterKilledIcon[i-1].getHeightScaled());
				
				monsterKilledIcon[i] = new Sprite(0, 0, monsterKilledTextureRegion.clone());
				monsterKilledIcon[i].setPosition(2*CAMERA_WIDTH/3, monsterKilledIcon[i-1].getY() + monsterKilledIcon[i-1].getHeightScaled());
//				monsterKilledIcon[i].setScale(0.5f);
				monsterKilledIcon[i].setSize(20, 20);
				scene.attachChild(monsterKilledIcon[i]);
				
				monsterKilledByPlayer[i] = new ChangeableText(0, 0, fontScore, "| " + rankPlayer.getNumberKill());
				monsterKilledByPlayer[i].setPosition(monsterKilledIcon[i].getX() + monsterKilledIcon[i].getWidthScaled(), monsterKilledIcon[i-1].getY() + monsterKilledIcon[i-1].getHeightScaled());
				
				scene.attachChild(namePlayer[i]);
				scene.attachChild(scorePlayer[i]);
				scene.attachChild(monsterKilledByPlayer[i]);
			}
		}
		
		final Sprite backSprite = new Sprite(0, 0, backTextureRegion);
		backSprite.setSize(50, 50);
		backSprite.setPosition(CAMERA_WIDTH - backSprite.getWidthScaled(), CAMERA_HEIGHT - backSprite.getHeightScaled());
		scene.attachChild(backSprite);
		
		scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
			
			@Override
			public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
				Region backRegion = new Region(backSprite.getX(), backSprite.getY(), backSprite.getWidthScaled(), backSprite.getHeightScaled());
				if (backRegion.isCovered(new Cordinate(pSceneTouchEvent.getX(), pSceneTouchEvent.getY()), backRegion)) {
					startActivity(new Intent(getBaseContext(), MainMenu.class));
					finish();
					return true;
				}
				return false;
			}
		});
		
		return scene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}
	
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(new Intent(this, MainMenu.class));
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	public static ArrayList<RankView> listRanking(){
		rankViewDAO.getReadable();
		return rankViewDAO.getListRankView();
	}

}
